﻿// RequiredPublic.cpp : 实现文件
//

#include "stdafx.h"
#include "学生管理系统MFC.h"
#include "RequiredPublic.h"
#include "afxdialogex.h"


// CRequiredPublic 对话框

IMPLEMENT_DYNAMIC(CRequiredPublic, CDialogEx)

CRequiredPublic::CRequiredPublic(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_REQUIREDPUBLIC, pParent)
{

}

CRequiredPublic::~CRequiredPublic()
{
}

void CRequiredPublic::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_combobox);
	DDX_Control(pDX, IDC_COMBO2, m_combobox2);
	DDX_Control(pDX, IDC_LIST1, m_list);
}


BEGIN_MESSAGE_MAP(CRequiredPublic, CDialogEx)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST1, &CRequiredPublic::OnNMDblclkList1)
	ON_BN_CLICKED(IDC_BUTTON1, &CRequiredPublic::OnBnClickedButton1)
	ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()


// CRequiredPublic 消息处理程序


BOOL CRequiredPublic::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	if (m_term == "上")
	{
		if (m_year - m_grade == 0)
		{

			m_combobox.InsertString(0, _T("基础英语1"));
			m_combobox.InsertString(1, _T("大学英语2"));
			m_combobox.InsertString(2, _T("高级英语1"));
		}
		else
		{

			m_combobox.InsertString(0, _T("基础英语3"));
			m_combobox.InsertString(1, _T("大学英语4"));
			m_combobox.InsertString(2, _T("高级英语5"));
		}
		m_combobox.InsertString(3, _T("体育(初级)"));
	}
	else
	{
		if (m_year - m_grade == 0)
		{

			m_combobox.InsertString(0, _T("基础英语2"));
			m_combobox.InsertString(1, _T("大学英语3"));
			m_combobox.InsertString(2, _T("高级英语2"));
		}
		else
		{

			m_combobox.InsertString(0, _T("基础英语4"));
			m_combobox.InsertString(1, _T("大学英语5"));
			m_combobox.InsertString(2, _T("高级英语4"));
		}
		m_combobox.InsertString(3, _T("体育(高级)"));
	}
	m_combobox.SetCurSel(0);
	m_combobox2.SetCurSel(0);
	CRect rect;
	// 获取编程语言列表视图控件的位置和大小   
	m_list.GetClientRect(&rect);
	// 为列表视图控件添加全行选中和栅格风格   
	m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_list.InsertColumn(0, _T("课头号"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(1, _T("课程名"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(2, _T("学分"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(3, _T("剩余/最大人数"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(4, _T("教师名"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(5, _T("职称"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(6, _T("授课学院	"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(7, _T("班级"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(8, _T("教材"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(9, _T("学年"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(10, _T("学期"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(11, _T("上课时间地点"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(12, _T("备注"), LVCFMT_CENTER, rect.Width() / 3, 0);
	Query();
	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CRequiredPublic::OnNMDblclkList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	//获取选中的的一个位置
	POSITION p = m_list.GetFirstSelectedItemPosition();
	if (p == NULL)
	{
		TRACE("没有行被选中!");
		return;
	}
	//获取刚选取的位置的下标(从0开始的) 
	int index = m_list.GetNextSelectedItem(p);

	//获得选中的内容
	//得到第index行.第0列的内容(下标从0开始)
	CString 课头号 = m_list.GetItemText(index, 0);
	CString sql = _T("SELECT 课程.剩余人数 FROM 成绩,课程 WHERE [成绩].[课头号]=[课程].[课头号] AND [成绩].[学号] = ") + m_username + _T(" AND [成绩].[课头号] = ") + 课头号;
	::CoInitialize(NULL);
	g_pDB->Initialize(ConnectionAddress, ConnectionDB, ConnectionUser, ConnectionPassword);
	CSqlStatement *stmt = g_pDB->ExecuteSQL(sql);
	if (stmt->NextRow() != NULL)
	{
		MessageBox(_T("该门课程已选！"));
		delete stmt;
		::CoUninitialize();
		return;
	}
	else
	{
		sql = _T("SELECT 剩余人数 FROM 课程 WHERE 课头号 = ") + 课头号;
		stmt = g_pDB->ExecuteSQL(sql);
		CString ss;
		if (stmt != NULL && stmt->NextRow())
			ss = stmt->ValueString(_T("剩余人数"));
		int 剩余人数 = _ttoi(ss);
		if (剩余人数 <= 0)
		{
			MessageBox(_T("选课人数已满！"));
			delete stmt;
			::CoUninitialize();
			return;
		}
	}
	sql = _T("INSERT INTO [教务系统].[dbo].[成绩] ([课头号], [学号], [学年], [学期]) OUTPUT INSERTED.[id], INSERTED.[课头号], INSERTED.[学号], INSERTED.[成绩], INSERTED.[年级], INSERTED.[学期] VALUES (") + 课头号 + _T(", ") + m_username + _T(", '") + m_year + _T("', '") + m_term + _T("')");
	g_pDB->UpdateSQL(sql);
	sql = _T("UPDATE [教务系统].[dbo].[课程] SET [剩余人数]=剩余人数-1 WHERE ([课头号]=" + 课头号 + _T(")"));
	g_pDB->UpdateSQL(sql);
	delete stmt;
	::CoUninitialize();
	Query();
	MessageBox(_T("选课成功！"));
	*pResult = 0;
}

void CRequiredPublic::Query()
{
	m_list.DeleteAllItems();
	CString 课程, 校区, 课程名;
	((CComboBox*)GetDlgItem(IDC_COMBO1))->GetWindowText(课程);
	((CComboBox*)GetDlgItem(IDC_COMBO2))->GetWindowText(校区);
	((CComboBox*)GetDlgItem(IDC_EDIT1))->GetWindowText(课程名);
	CString sql;
	sql = _T("SELECT * FROM [课程] ,[教师] WHERE [教师].[工号] = [课程].[教师] AND [课程].[课程类型]='公共必修' AND 课程.公共必修类型='")+ 课程 +_T("'");
	if (校区 != _T("全校"))
		sql += _T(" AND dbo.[课程].[上课校区]='") + 校区 + _T("'");
	if (课程名 != _T(""))
		sql += _T(" AND [课程].[课程名] LIKE '%") + 课程名 + _T("%'");
	::CoInitialize(NULL);
	g_pDB->Initialize(ConnectionAddress, ConnectionDB, ConnectionUser, ConnectionPassword);
	CSqlStatement *stmt = g_pDB->ExecuteSQL(sql);
	while (stmt != NULL)
	{
		if (stmt->NextRow() == NULL)
		{
			break;
		}
		int nRow = m_list.InsertItem(0, stmt->ValueString(_T("课头号")));//插入行
		m_list.SetItemText(nRow, 1, stmt->ValueString(_T("课程名")));//插入行
		m_list.SetItemText(nRow, 2, stmt->ValueString(_T("学分")));//设置数据
		CString 剩余最大人数 = stmt->ValueString(_T("剩余人数")) + _T("/") + stmt->ValueString(_T("最大人数"));
		m_list.SetItemText(nRow, 3, 剩余最大人数);//设置数据
		m_list.SetItemText(nRow, 4, stmt->ValueString(_T("姓名")));//设置数据
		m_list.SetItemText(nRow, 5, stmt->ValueString(_T("职称")));//设置数据
		m_list.SetItemText(nRow, 6, stmt->ValueString(_T("授课学院")));//设置数据
		m_list.SetItemText(nRow, 7, stmt->ValueString(_T("班级")));//设置数据
		m_list.SetItemText(nRow, 8, stmt->ValueString(_T("教材")));//设置数据
		m_list.SetItemText(nRow, 9, stmt->ValueString(_T("学年")));//设置数据
		m_list.SetItemText(nRow, 10, stmt->ValueString(_T("学期")));//设置数据
		CString 上课时间地点 = stmt->ValueString(_T("上课时间")) + _T(",") + stmt->ValueString(_T("上课校区")) + _T(",") + stmt->ValueString(_T("上课地点"));
		m_list.SetItemText(nRow, 11, 上课时间地点);//设置数据
		m_list.SetItemText(nRow, 12, stmt->ValueString(_T("备注")));//设置数据
																  //m_list.SetItemText(nRow, 12, stmt->ValueString(_T("状态")));//设置数据
	}
	delete stmt;
	::CoUninitialize();
}

void CRequiredPublic::OnBnClickedButton1()
{
	Query();
	// TODO: 在此添加控件通知处理程序代码
}


void CRequiredPublic::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialogEx::OnShowWindow(bShow, nStatus);
	if (bShow == true)
		Query();
	// TODO: 在此处添加消息处理程序代码
}
void CRequiredPublic::OnOK()
{
	OnBnClickedButton1();
}